package com.qf.service.impl;

import com.qf.constans.ResultCode;
import com.qf.entity.CheckType;
import com.qf.entity.TSmsTemplate;
import com.qf.entity.TemplateParam;
import com.qf.exceptions.AddDataException;
import com.qf.mapper.SmsTempMapper;
import com.qf.mapper.TemplateParamMapper;
import com.qf.service.SmsTempService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/*
杨威
2021/10/29
21:15
*/
@Service
@Transactional
public class SmsTempServiceImpl implements SmsTempService {
    @Autowired
    private TemplateParamMapper templateParamMapper;
    private SmsTempMapper smsTempMapper;
    @Autowired
    public void setSmsTempMapper(SmsTempMapper smsTempMapper) {
        this.smsTempMapper = smsTempMapper;
    }

    @Override
    public void addSmsTemp(TSmsTemplate tSmsTemplate) {
        if(tSmsTemplate.isNull(CheckType.ADD)){
            throw new AddDataException("必须传递的数据没传递", ResultCode.DATA_ISNULL);
        }
        //不为空执行添加
        smsTempMapper.addTemp(tSmsTemplate);
    }

    @Override
    public List<TSmsTemplate> findByMultiParam(TSmsTemplate tSmsTemplate) throws Exception {
        TemplateParam templateParam = templateParamMapper.findByname("1");
        String param = templateParam.getParam();
        String[] split = param.split(",");
        List<String> strings = new ArrayList<>();
        for (String s : split) {
            strings.add(s);
        }
        Class<? extends TSmsTemplate> aClass = tSmsTemplate.getClass();
        //根据类对象获得模板对象
        TSmsTemplate instance = aClass.newInstance();

        //从传递的参数对象中 取出我们需要的参数 设置给我们新创建的对象
        strings.forEach(name -> {
                    try {
                        //从指定类中获取指定名字的属性
                        PropertyDescriptor propertyDescriptor=new PropertyDescriptor(name,aClass);
                        Method readMethod = propertyDescriptor.getReadMethod();
                        Method writeMethod = propertyDescriptor.getWriteMethod();
                        writeMethod.invoke(instance,readMethod.invoke(tSmsTemplate));
                    } catch (IntrospectionException e) {

                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
        );
        System.out.println("instance = " + instance);

        List<TSmsTemplate> smsTemplates = this.smsTempMapper.findByMultiParam(instance);

        for (TSmsTemplate smsTemplate : smsTemplates) {
            System.out.println("smsTemplate = " + smsTemplate);
        }

        return smsTemplates;
    }
}
